<?php namespace abc777;
// +----------------------------------------------------------------------
// | 会员相关类
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/05/20
// +----------------------------------------------------------------------
// | 特别说明：个人文件，请勿修改，如需修改，请采用类的继承或联系本人
// +----------------------------------------------------------------------
class Member{
	
	public function __construct(){
		
	}
	
	
	// 会员资金查询
	public static function Fund($s, $ids=0, $type=0, $aid=NULL){
		
		// $funds = array();
		
		// 如果 type=3 则可供应+授信
		// $merge = ($type==3) ? true:false;
		// $ids = ($ids==0) ? $_SESSION['sid']:$ids;
		// $simxml = $s->PFT_Member_Fund($ids, $type, $aid);
		// $simxml_o = simplexml_load_string($simxml);
		// foreach($simxml_o as $row){
			// $funds[(int)$row->UUfid] = (string)$row->UUkmoney/100;
		// }

		// if($type==1){
			
		// }
		
		// if($type)
		
		// return $funds;
	}
	
	
	// 获取会员的供应商
	public static function Supplier($sid=0){
		
		$sid = $sid ? $sid:$_SESSION['sid'];
		$suppliers = array();
		$sql = "select r.id,r.parent_id,m.dname from pft_member_relationship r left join pft_member m on r.parent_id=m.id where r.son_id=$sid and r.ship_type=0 and r.status=0";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $suppliers[$row['parent_id']] = $row['dname'];
		return $suppliers;
	}
	
	// 获取会员的分销商
	public static function Distributor($sid=0){
		$sid = $sid ? $sid:$_SESSION['sid'];
		$distributors = array();
		$sql = "select r.id,r.son_id,m.dname from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$sid and r.ship_type=0 and r.status=0";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$distributors[$row['son_id']] = $row['dname'];
		}
		return $distributors;
	}
	 
	 
	// 会员关系相关操作
	public static function RelationShip($parent_id, $son_id){	
		// 判断是否存在记录
		$sql = "select id,status from pft_member_relationship where parent_id=$parent_id and son_id=$son_id and son_id_type=0 and ship_type=0 limit 1";
		$GLOBALS['le']->query($sql);
		if($GLOBALS['le']->fetch_assoc()){
			
			$sql = "update pft_product_sale_list set status=0 where fid=$son_id and aid=$parent_id limit 1";
			$GLOBALS['le']->query($sql);
			
			if($GLOBALS['le']->f('status')==0) return true;// 已关联
			$rid = $GLOBALS['le']->f('id');// 重新关联
			$sql = "update pft_member_relationship set status=0 and restoretime=now() where id=$rid limit 1";
			return $GLOBALS['le']->query($sql) ? true:false;
		}else{// 不存在记录
			$sql = "insert into pft_member_relationship set parent_id='$parent_id',son_id='$son_id',son_id_type=0,ship_type=0,rectime=now()";
			if(!$GLOBALS['le']->query($sql)) return false;
			$ins = "insert into pft_product_sale_list set fid=$son_id,aid=$parent_id,pids=''";
			$GLOBALS['le']->query($ins);
		}
		return true;
	}

	
	
	
	
	// 获取销售产品
	public static function saleProds(){
		$clsResult = array('count'=>0, 'prods'=>array());
		$sid = $sid ? $sid:$_SESSION['sid'];
		$sql = "select * from pft_p_apply_evolute where fid=$sid and status=0 and sourceid<>$sid";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$aids = explode(',', $row['aids']);
			if(in_array($sid,$aids) && end($aids)!=$sid){
				$clsResult['count'] += 1;
				$clsResult['prods'][] = $row;
			}
		}
		return $clsResult;
	}
	
	
}
